<!-- 清单车 -->
<template>
  <div class="mechanicalCart">
    <NavBarHeader class="navBarHeader"></NavBarHeader>
    <div class="container">
      <img
        src="./../../../../../public/img/cart/cartTitleIcon.png"
        class="cartTitle"
      />
      <div class="contentList" v-if="goDetailedData.length >= 1">
        <div class="listTitle">寻租机械</div>
        <div v-for="(item, index) of goDetailedData" :key="index">
          <el-form>
            <el-checkbox-group
              v-model="ItemcheckBox"
              class="listBox"
              @change="
                btn(
                  item.name,
                  item.size_id,
                  item.type_id,
                  item.first_num,
                  item.listData,
                  item.pid
                )
              "
            >
              <el-checkbox :label="index" name="type" class="listItem">
                <div class="itemInfo">
                  <div class="imgBox">
                    <img :src="item.img" class="mechanicaImg" />
                  </div>
                  <span class="itemName">{{ item.name }}</span>
                </div>
              </el-checkbox>
              <img
                src="./../../../../../public/img/cart/closeIcon.png"
                class="closeIcon"
                @click="deleteOneNode(item)"
              />
            </el-checkbox-group>
          </el-form>
        </div>
        <div class="goPrice" @click="goSee">去询价</div>
      </div>
      <div class="noMessage" v-if="goDetailedData.length < 1">
        <img
          src="./../../../../../public/img/home/noMessage.png"
          class="noMessageImg"
        />
      </div>
    </div>
    <FooterBar class="footerBar"></FooterBar>
  </div>
</template>

<script >
import NavBarHeader from "../../../../components/NavBarHeader";
import FooterBar from "../../../../components/FooterBar";
import { Message } from "element-ui";
export default {
  // import引入的组件需要注入到对象中才能使用
  components: {
    NavBarHeader,
    FooterBar,
  },
  data() {
    // 这里存放数据
    return {
      goDetailedData: [],
      myValue: [],
      ItemcheckBox: [],
      number: 1,
      trueLabel: "",
      userInfo: {},
    };
  },
  // 过滤器
  filters: {},
  // 监听属性 类似于data概念
  computed: {},
  // 监控data中的数据变化
  watch: {
    // 监听缓存变化
    goDetailedData: {
      // 默认写成函数，就相当于写了个handler(),deep:true,是否深度监控
      handler() {
        // localStorage 默认存的是字符串
        localStorage.setItem("listData", JSON.stringify(this.goDetailedData));
      },
      deep: true,
    },
  },
  // 方法集合
  methods: {
    unique(obj) {
      var uniques = [];
      var stringify = {};
      for (var i = 0; i < obj.length; i++) {
        var keys = Object.keys(obj[i]);
        keys.sort(function (a, b) {
          return Number(a) - Number(b);
        });
        var str = "";
        for (var j = 0; j < keys.length; j++) {
          str += JSON.stringify(keys[j]);
          str += JSON.stringify(obj[i][keys[j]]);
        }
        if (!stringify.hasOwnProperty(str)) {
          uniques.push(obj[i]);
          stringify[str] = true;
        }
      }
      uniques = uniques;
      return uniques;
    },
    // 删除某一项
    deleteOneNode(val) {
      this.goDetailedData = this.goDetailedData.filter((item) => item !== val);
    },
    goSee() {
      if (this.userInfo) {
        if (this.ItemcheckBox.length <= 0) {
          Message.error("请先选择机械！");
          return;
        }
        this.myValue.forEach((items, index) => {
          items.typeSize.forEach((item, index) => {
            item = Number(item);
          });
        });
        this.$router.push({
          name: "MechanicalRent",
          params: { myValue: this.myValue },
        });
      } else {
        this.$router.push({ path: "/loginPc" });
      }
    },
    // 拿到checkbox点击的数据复制给data里别的地方在取值
    btn(typeName, size_id, type_id, first_num, listData, pid) {
      console.log(this.myValue);
      let typeBox = [];
      console.log(pid);
      typeBox.push(first_num, pid, size_id);
      this.myValue.push({
        typeSize: typeBox,
        number: this.number,
        typeName: typeName,
        type_id: type_id,
        listData: listData,
        unit: this.unit,
      });
    },
  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.goDetailedData =
      JSON.parse(localStorage.getItem("listData")) || this.goDetailedData;
    console.log(this.goDetailedData);
    this.goDetailedData = this.unique(this.goDetailedData);
    console.log(this.goDetailedData);
    this.userInfo = window.localStorage.getItem("userInfo");
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, // 生命周期 - 创建之前
  beforeMount() {}, // 生命周期 - 挂载之前
  beforeUpdate() {}, // 生命周期 - 更新之前
  updated() {}, // 生命周期 - 更新之后
  beforeDestroy() {}, // 生命周期 - 销毁之前
  destroyed() {}, // 生命周期 - 销毁完成
  ctivated() {}, // 如果页面有keep-alive缓存功能，这个函数会触发
};
</script>

<style lang='scss' scoped>
.navBarHeader {
  background: #1c1e2b;
  z-index: 1000;
}
.container {
  width: 1180px;
  margin: 0 auto 0 auto;
  padding-top: 70px;
  padding-bottom: 50px;
  box-sizing: border-box;
}
.cartTitle {
  width: 183px;
  height: 68px;
  padding-top: 40px;
}
.contentList {
  margin-top: 50px;
}
.listTitle {
  width: 100%;
  height: 40px;
  line-height: 40px;
  background: #f5f5f5;
  color: #333;
  font-size: 12px;
  padding-left: 20px;
  box-sizing: border-box;
}
.imgBox {
  width: 90px;
  height: 90px;
  background: #f5f5f5;
  border-radius: 2px;
  position: relative;
}
.mechanicaImg {
  width: 80px;
  height: 66px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.listItem {
  display: flex;
  align-items: center;
  margin-left: 20px;
  margin-top: 5px;
}
.listBox {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 20px;
  width: 100%;
  height: 130px;
  background: #ffffff;
  border: 1px solid #e7e7e7;
}
.itemName {
  margin-left: 20px;
  font-size: 14px;
  color: #333;
}
.itemInfo {
  display: flex;
  align-items: center;
}
.closeIcon {
  width: 18px;
  height: 18px;
  margin-right: 20px;
  cursor: pointer;
}
.goPrice {
  width: 600px;
  height: 40px;
  line-height: 40px;
  background: linear-gradient(136deg, #ffb492 0%, #ff7d59 100%);
  border-radius: 4px;
  color: #fff;
  font-size: 14px;
  text-align: center;
  margin: 50px auto 0 auto;
}
.noMessage {
  width: 100%;
  height: 100vh;
}
.noMessageImg {
  width: 262px;
  height: 335px;
  position: absolute;
  top: 55%;
  left: 50%;
  transform: translate(-50%, -50%);
}
</style>
